const app = getApp()
const request = require('../../utils/request.js');
const {
  processImageUrl
} = require('../../utils/image')
Page({
  data: {
    historyList: [],
    page: 1,
    pageSize: 10,
    hasMore: true,
    loading: false
  },

  onLoad() {
    this.loadHistoryList()
  },

  // 加载足迹列表
  loadHistoryList() {
    if (this.data.loading || !this.data.hasMore) return
    this.setData({
      loading: true
    })
    // 模拟API请求
    return new Promise((resolve, reject) => {
      request.request({
        url: `${app.globalData.baseUrl}/api/view-records/user`,
        method: 'GET',
        header: {
          'Authorization': `Bearer ${app.getGlobalToken()}`
        },
        success: (res) => {
          const {
            list,
            total
          } = res.data.data
          // 使用工具函数处理图片URL
          list.map((item) => {
            item.residue.image = processImageUrl(item.residue.image)
            return item
          })
          this.setData({
            historyList: [...this.data.historyList, ...list],
            hasMore: this.data.historyList.length + list.length < total,
            loading: false
          })
          resolve()
        },
        fail: reject
      })
    })
  },

  // 切换类型
  switchType(e) {
    const type = e.currentTarget.dataset.type
    if (type === this.data.currentType) return

    this.setData({
      currentType: type,
      historyList: [],
      page: 1,
      hasMore: true
    }, () => {
      this.loadHistoryList()
    })
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.setData({
      historyList: [],
      page: 1,
      hasMore: true
    }, () => {
      this.loadHistoryList()
      wx.stopPullDownRefresh()
    })
  },

  // 上拉加载更多
  onReachBottom() {
    if (this.data.hasMore) {
      this.setData({
        page: this.data.page + 1
      }, () => {
        this.loadHistoryList()
      })
    }
  },

  // 收藏/取消收藏
  toggleFavorite(e) {
    const {
      id,
      type
    } = e.currentTarget.dataset
    const item = this.data.historyList.find(item => item.id === id)
    if (!item) return

    const isFavorite = !item.isFavorite
    const favorites = isFavorite ? item.favorites + 1 : item.favorites - 1

    // 更新列表数据
    const historyList = this.data.historyList.map(item => {
      if (item.id === id) {
        return {
          ...item,
          isFavorite,
          favorites
        }
      }
      return item
    })

    this.setData({
      historyList
    })

    // 显示提示
    wx.showToast({
      title: isFavorite ? '收藏成功' : '已取消收藏',
      icon: 'success'
    })
  },

  // 清空足迹
  clearHistory() {
    wx.showModal({
      title: '提示',
      content: '确定要清空所有浏览记录吗？',
      success: (res) => {
        if (res.confirm) {
          this.setData({
            historyList: [],
            page: 1,
            hasMore: true
          })
          wx.showToast({
            title: '清空成功',
            icon: 'success'
          })
        }
      }
    })
  },

  // 跳转到详情页
  goToDetail(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/supply/detail/detail?id=${id}`
    })
  }
})